package com.testdocs.client.structure.menu;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceChangeEvent;
import com.testdocs.client.ClientFactory;
import com.testdocs.client.project.dashboard.DashboardPlace;
import com.testdocs.client.version.list.VersionListPlace;

public class MenuPanelPresenter implements MenuPanel.Presenter {
	private ClientFactory clientFactory;
	
	public MenuPanelPresenter(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		clientFactory.getEventBus().addHandler(PlaceChangeEvent.TYPE, new PlaceChangedHandler());
	}
	
	@Override
	public void onSelectDashboard() {
		Place place = new DashboardPlace(clientFactory.getProjectContextHolder().getProject().getProjectId());
		clientFactory.getPlaceController().goTo(place);
	}

	@Override
	public void onSelectVersionList() {
		Place place = new VersionListPlace(clientFactory.getProjectContextHolder().getProject().getProjectId());
		clientFactory.getPlaceController().goTo(place);		
	}
	
	@Override
	public void onOpenTestDocs() {
		clientFactory.getTestGroupMenu().onOpenTestGroupsMenu();
	}

	public class PlaceChangedHandler implements PlaceChangeEvent.Handler {
		@Override
		public void onPlaceChange(PlaceChangeEvent event) {
			Place place = event.getNewPlace();
			if (place instanceof RequiresMenuSelection) {
				clientFactory.getMenuPanel().selectMenuItem(((RequiresMenuSelection)place).getMenuItem());
			}
		}
	}
}
